package com.sgai.zero_robot.utils;

import android.os.Environment;
import android.util.Log;

import com.sgai.nest.utils.LogUtils;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

public class LogUtil {
    private static final String TAG = "LogUtils";
    private static boolean isDebug = true;
    private static AtomicBoolean isInit = new AtomicBoolean();
    //文件地址
    public static String rootPath =
            Environment.getExternalStorageDirectory().getPath() + "/检零机器人/";
    public static String logPath = rootPath + "/Log/"; //飞机日志保存路径

    private static SimpleDateFormat logInfoFormat = new SimpleDateFormat("yyyy-MM-dd");
    public static void init(){
        if (isInit.get()){
            return;
        }
        isInit.set(true);
        //由于这里包装了一层即当前类  所以填写1  如果两层得填写2
        LogUtils.init(1);
        new Thread(new Runnable(){
            @Override
            public void run() {
                File file = new File(logPath);
                if (!file.exists()){
                    file.mkdirs();
                }else if (!file.isDirectory()){
                    file.delete();
                    file.mkdirs();
                }
                File[] files = file.listFiles();
                List<Long> list = getFileLong(files);
                if (files == null){

                }else if (files.length == 7){
                    if ((logInfoFormat.format(new Date(list.get(list.size()-1)))).equals(logInfoFormat.format(new Date(System.currentTimeMillis())))){
                        //如果最后一个文件和要生成的文件相同 那么就可以保留7个

                    }else{
                        //如果最后一个文件不是要生成的文件 就需要删除了第一个
                        String format = logInfoFormat.format(new Date(list.get(0)))+".txt";
                        File file2 = new File(logPath,format);
                        Log.d(TAG, "run0: "+file2.getAbsolutePath());
                        file2.delete();
                    }
                }else if (files.length>7){
                    int endIndex = 0;
                    if ((logInfoFormat.format(new Date(list.get(list.size()-1)))).equals(logInfoFormat.format(new Date(System.currentTimeMillis())))){
                        endIndex = list.size()-7;
                    }else{
                        endIndex = list.size()-6;
                    }
                    for (int i = 0;i<endIndex;i++){
                        String format = logInfoFormat.format(new Date(list.get(i)))+".txt";
                        File file2 = new File(logPath,format);
                        Log.d(TAG, "run1: "+file2.getAbsolutePath());
                        file2.delete();
                    }
                }


                String path = logPath + logInfoFormat.format(new Date(System.currentTimeMillis())) + ".txt";
                LogUtils.setPath(path);
            }
        }).start();


    }
    private static  List<Long> getFileLong(File[] files){
        List<Long> list = new ArrayList<Long>();
        if (files==null){
            return list;
        }
        for ( File file1:files){
            String name = file1.getName().replace(".txt","");
            Date date = null;
            try {
                date = logInfoFormat.parse(name);
                list.add(date.getTime());
            } catch (ParseException e) {
                e.printStackTrace();
                LogUtil.e(e,Thread.currentThread());
            }
        }
        Collections.sort(list);
        return list;
    }
    public static void v(String tag, String msg) {
        if (isDebug) {
            Log.v(tag, msg);
        }
        LogUtils.print(tag+" : "+msg);
    }

    public static void v(String msg) {
        if (isDebug) {
            Log.v(TAG, msg);
        }
        LogUtils.print(msg);
    }

    public static void d(String msg) {
        if (isDebug) {
            Log.d(TAG, msg);
        }
        LogUtils.print(msg);
    }

    public static void d(String tag, String msg) {
        if (isDebug) {
            Log.d(tag, msg);
        }
        LogUtils.print(tag+" : "+msg);
    }

    public static void i(String msg) {
        if (isDebug) {
            Log.i(TAG, msg);
        }
        LogUtils.print(msg);
    }

    public static void i(String tag, String msg) {
        if (isDebug) {
            Log.i(tag, msg);
        }
        LogUtils.print(tag+" : "+msg);
    }

    public static void w(String msg) {
        if (isDebug) {
            Log.w(TAG, msg);
        }
        LogUtils.print(msg);
    }

    public static void w(String tag, String msg) {
        if (isDebug) {
            Log.w(tag, msg);
        }
        LogUtils.print(tag+" : "+msg);
    }

    public static void e(String msg) {
        if (isDebug) {
            Log.e(TAG, msg);
        }
        LogUtils.print(msg);
    }

    public static void e(String tag, String msg) {
        if (isDebug) {
            Log.e(tag, msg);
        }
        LogUtils.print(tag+" : "+msg);
    }
    public static void e(Throwable e, Thread thread) {
        if (isDebug) {
            e.printStackTrace();
        }
        LogUtils.print(e,thread);
    }
    public static void e(byte[] bytes) {
        if (bytes == null) {
            return;
        }
        StringBuilder sb = new StringBuilder();
        for (byte b : bytes) {
            // 将每个 byte 转换为十六进制字符串，%02x 表示以两位十六进制小写形式输出，不足两位补 0
            sb.append(String.format("%02x ", b));
        }
        LogUtil.e("printHex", sb.toString());
    }
}
